﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;

namespace Common.DbHelper.HelperException
{

    /// <summary>
    /// DataHelper异常处理者
    /// </summary>
    /// <param name="e">异常源</param>
    public delegate void ExceptionHandler(DbHelpException e);

    /// <summary>
    /// 异常类型
    /// </summary>
    public enum ExceptionType
    {
        /// <summary>
        /// 建立连接异常 
        /// </summary>
        BuildConnection,

        /// <summary>
        /// 建立连接并打开连接时异常 
        /// </summary>
        BuildConnectionAndOpen,

        /// <summary>
        /// 建立命令异常
        /// </summary>
        BuildCommand,

        /// <summary>
        /// 执行返回单行命令时异常
        /// </summary>
        ExecuteScalar,

        /// <summary>
        /// 执行返回单行泛型命令时异常
        /// </summary>
        ExecuteGenericScalar,

        /// <summary>
        /// 执行返回泛型实体时异常
        /// </summary>
        ExecuteGenericEntity,

        /// <summary>
        /// 执行命令时异常
        /// </summary>
        ExecuteCommand,

        /// <summary>
        /// 读取数据异常
        /// </summary>
        GetDataReader
    }

    /// <summary>
    /// 作者：赵虎
    /// 时间：2010-1-1
    /// 功能：Db基础异常处理
    /// </summary>
    public class DbHelpException
    {
        /// <summary>
        /// 运行时的异常源
        /// </summary>
        private Exception _exception;

        /// <summary>
        /// 运行时的源命令
        /// </summary>
        private DbCommand _command;

        /// <summary>
        /// 运行时的读写对像
        /// </summary>
        private DbDataReader _dataReader;

        /// <summary>
        /// 运行时的异常类型
        /// </summary>
        private ExceptionType _exceptionType;

        /// <summary>
        /// 运行时连接字符串
        /// </summary>
        private string _connectionString;

        /// <summary>
        /// 运行时的连接
        /// </summary>
        private DbConnection _connection;

        /// <summary>
        /// 运行时的适配器对像
        /// </summary>
        private DbDataAdapter _adapter;

        /// <summary>
        /// 运行时的异常源
        /// </summary>
        public Exception Exception
        {
            get { return _exception; }
            set { _exception = value; }
        }

        /// <summary>
        /// 运行时的源命令
        /// </summary>
        public DbCommand Command
        {
            get { return _command; }
            set { _command = value; }
        }

        /// <summary>
        /// 运行时的读写对像
        /// </summary>
        public DbDataReader DataReader
        {
            get { return _dataReader; }
            set { _dataReader = value; }
        }

        /// <summary>
        /// 运行时的异常类型
        /// </summary>
        public ExceptionType ExceptionType
        {
            get { return _exceptionType; }
            set { _exceptionType = value; }
        }

        /// <summary>
        /// 运行时连接字符串
        /// </summary>
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }

        /// <summary>
        /// 运行时的连接
        /// </summary>
        public DbConnection Connection
        {
            get { return _connection; }
            set { _connection = value; }
        }

        /// <summary>
        /// 运行时的适配器对像
        /// </summary>
        public DbDataAdapter Adapter
        {
            get { return _adapter; }
            set { _adapter = value; }
        }
    }
}
